package com.cnhis.cloudhealth.clinical.clidoctor.clidoctor.bo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.clidoctor.clidoctor.dao.CliDoctorDao;
import com.cnhis.cloudhealth.clinical.clidoctor.clinicalreceptiontransfer.bo.ClinicalReceptionBo;
import com.cnhis.cloudhealth.clinical.clidoctor.cliparamter.dao.CliParamterDao;
import com.cnhis.cloudhealth.clinical.clidoctor.clisendadvice.dao.CliSendAdviceDao;
import com.cnhis.cloudhealth.clinical.clidoctor.patientsearch.dao.PatientSearchDao;
import com.cnhis.cloudhealth.clinical.hosdoctor.clinicalpathway.service.PathwayManageImpl;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.cardmember.bo.CardMemberMangerBo;
import com.cnhis.cloudhealth.commons.exception.ExBaseException;
import com.cnhis.cloudhealth.commons.interceptor.CLoginFilter;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.jpinyin.PinyinHelper;
import com.cnhis.cloudhealth.rate.base.vaa1.dao.RateVaa1Dao;
import com.cnhis.cloudhealth.rate.base.vac4.bo.Vac4Bo;

import net.sf.json.JSONObject;

@Service
public class CliDoctorBo
{
    @Resource
    private CliDoctorDao cliDoctorDao;
    
    @Resource
    private RateVaa1Dao ratevaa1Dao;
    
    @Autowired
    private GetNewId getNewId;
    @Autowired
    private Vac4Bo vac4Bo;
    @Autowired
    private ClinicalReceptionBo clinicalReceptionBo;
    @Autowired
    private CardMemberMangerBo cardMemberMangerBo;
    @Resource
    private CliParamterDao cliParamterDao;
    @Resource
    private PatientSearchDao dao;
    @Resource
    private CliSendAdviceDao cliSendAdviceDao;
    
    public ModelVo getDoctorPosition(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            map = cliDoctorDao.getDoctorPosition(map);
            mo.setMap(map);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
    }
  
    /**
     * 生成条码和诊疗申请单
     * @param map
     * @return
     */
    public ModelVo exec_nurse_Lis_Bills_Print(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            insertCbw03();
            String para89 = getParameter(9999,9999,89);
            para89 = (para89==null?"":para89);
            List cbm01List = JsonUtil.getList(map.get("cbm01Arr").toString());
            for(int i = 0;i<cbm01List.size();i++)
            {
                Map temp =  (Map)cbm01List.get(i);
                String bda01 = temp.get("bda01").toString();
                Long cbm01 = Long.parseLong(temp.get("cbm01").toString());
                Map params = new HashMap();
                params.put("bda01", bda01);
                params.put("cbm01", cbm01);
                params.put("bce03", map.get("bce03").toString());
                //试管列表(门诊分检查和检验两种)
                List tempList = new ArrayList();
                if("L".equals(bda01))
                {
                    tempList = cliDoctorDao.getTempListL(params);
                    int cbw03I = cliDoctorDao.getCbw1();
                    //如果相同试管和相同样本相同医嘱单和相同执行科室，用相同的条码
                    List<Map<String, Object>> hasList = new ArrayList<Map<String, Object>>();
                    //得到条码的最后序列号
                    int cbw03 = 0;
                    for(int j = 0;j<tempList.size();j++)
                    {
                        Map bel1 = (Map<String, Object>)tempList.get(j);

                        String vbi21 = GlobalVariable.StringIsNull(hasBel1(hasList, bel1, cbw03, para89).get("vbi21"));
                        cbw03 = GlobalVariable.IntegerIsNull(hasBel1(hasList, bel1, cbw03, para89).get("cbw03"));
                        hasList.add(bel1);
                        updateCbw03(cbw03);
                        
                        bel1.put("vbq01", getNewId.nextId());
                        bel1.put("cbm01", cbm01);
                        bel1.put("bda01", bda01);
                        bel1.put("vbi21",vbi21);
                        //插入诊疗申请单
                        cliDoctorDao.insertVbq1(bel1);
                        //更新医嘱记录中的申请单id
                        cliDoctorDao.updateVaf1(bel1);
                        //更新抄送记录表
                        cliDoctorDao.updateVbi1(bel1);
                    }
                }
                else if("E".equals(bda01))
                {
                    tempList = cliDoctorDao.getTempListE(params);
                    int cbw03I = cliDoctorDao.getCbw1();
                    
                    List<Map<String, Object>> hasList = new ArrayList<Map<String, Object>>();
                    //得到条码的最后序列号
                    int cbw03 = 0;
                    for(int j = 0;j<tempList.size();j++)
                    {
                        Map bel1 = (Map)tempList.get(j);
                        //如果相同试管和相同样本相同医嘱单和相同执行科室，用相同的条码
                        String vbi21 = GlobalVariable.StringIsNull(hasBel2(hasList, bel1, cbw03, para89).get("vbi21"));
                        cbw03 = GlobalVariable.IntegerIsNull(hasBel2(hasList, bel1, cbw03, para89).get("cbw03"));
                        hasList.add(bel1);
                        updateCbw03(cbw03);
                        bel1.put("vbq01", getNewId.nextId());
                        bel1.put("cbm01", cbm01);
                        bel1.put("bda01", bda01);
                        bel1.put("vbi21",vbi21);
                        //插入诊疗申请单
                        cliDoctorDao.insertVbq1_E(bel1);
                        //更新医嘱记录中的申请单id
                        cliDoctorDao.updateVaf1_E(bel1);
                        //更新抄送记录表
                        cliDoctorDao.updateVbi1_E(bel1);
                    }
                }
                
            }
            mo.setMap(map);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
    private Map<String,Object> hasBel1(List<Map<String, Object>> hasList,Map<String, Object> bel1,int cbw03, String para89)
    {
        boolean flag = true;
        Map<String,Object> map = new HashMap<String,Object>();
        String vbi21 = "";
        for (int x = 0; x < hasList.size(); x++)
        {
            Map<String, Object> hasTemp = hasList.get(x);
            if ((GlobalVariable.StringIsNull(hasTemp.get("cbm01")).equals(GlobalVariable.StringIsNull(bel1.get("cbm01")))&&
                GlobalVariable.StringIsNull(hasTemp.get("vaf14")).equals(GlobalVariable.StringIsNull(bel1.get("vaf14")))&&
                GlobalVariable.StringIsNull(hasTemp.get("bck01b")).equals(GlobalVariable.StringIsNull(bel1.get("bck01b")))&&
                GlobalVariable.StringIsNull(hasTemp.get("bel01")).equals(GlobalVariable.StringIsNull(bel1.get("bel01")))
                ))
            {
                flag = false;
                vbi21 = GlobalVariable.StringIsNull(hasTemp.get("vbi21"));
                map.put("vbi21",vbi21);
                map.put("cbw03",cbw03);
                break;
            }
        }
        if(flag){
            cbw03 ++;
            vbi21 = para89 + new SimpleDateFormat("yyMMdd").format(new Date()) + "13" + getTemp9_14(cbw03) + cbw03;
            map.put("vbi21",vbi21);
            map.put("cbw03",cbw03);
        }
        return map;
    }
    
    private Map<String,Object> hasBel2(List<Map<String, Object>> hasList,Map<String, Object> bel1,int cbw03, String para89)
    {
        boolean flag = true;
        String vbi21 = "";
        Map<String,Object> map = new HashMap<String,Object>();
        for (int x = 0; x < hasList.size(); x++)
        {
            Map<String, Object> hasTemp = hasList.get(x);
            if ((GlobalVariable.StringIsNull(hasTemp.get("cbm01")).equals(GlobalVariable.StringIsNull(bel1.get("cbm01")))&&
                GlobalVariable.StringIsNull(hasTemp.get("bck01b")).equals(GlobalVariable.StringIsNull(bel1.get("bck01b")))&&
                GlobalVariable.StringIsNull(hasTemp.get("bbc01")).equals(GlobalVariable.StringIsNull(bel1.get("bbc01")))&&
                GlobalVariable.StringIsNull(hasTemp.get("vaf35")).equals(GlobalVariable.StringIsNull(bel1.get("vaf35")))
                ))
            {
                flag = false;
                vbi21 = GlobalVariable.StringIsNull(hasTemp.get("vbi21"));
                map.put("vbi21",vbi21);
                map.put("cbw03",cbw03);
                break;
            }
        }
        if(flag){
            cbw03 ++;
            vbi21 = para89 + new SimpleDateFormat("yyMMdd").format(new Date()) + "13" + getTemp9_14(cbw03) + cbw03;
            map.put("vbi21",vbi21);
            map.put("cbw03",cbw03);
        }
        return map;
    }
    
    /**
     * 生成条码和诊疗申请单
     * @param map
     * @return
     */
    public ModelVo exec_nurse_Lis_Bills_Print_batch(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            String para89 = getParameter(9999,9999,89);
            para89 = (para89==null?"":para89);
            List cbm01List1 = JsonUtil.getList(map.get("cbm01Arr").toString());
            List cbm01List = new ArrayList<>();
            int cbw03 = GlobalVariable.IntegerIsNull(map.get("cbw03"), 0);
            Set<String> set = new HashSet<String>();
            Map<String, Object> vbqMap = JsonUtil.getMap(GlobalVariable.StringIsNull(map.get("vbq1")));
            //cbmlist去重
            for(Map<String, Object> cbmMap : (List<Map<String, Object>>)cbm01List1)
            {
                if(!set.contains(GlobalVariable.StringIsNull(cbmMap.get("cbm01"))))
                {
                    cbm01List.add(cbmMap);
                    set.add(GlobalVariable.StringIsNull(cbmMap.get("cbm01")));
                }
            }
            for(int i = 0;i<cbm01List.size();i++)
            {
                Map temp =  (Map)cbm01List.get(i);
                String bda01 = temp.get("bda01").toString();
                Long cbm01 = Long.parseLong(temp.get("cbm01").toString());
                Map params = new HashMap();
                params.put("bda01", bda01);
                params.put("cbm01", cbm01);
                params.put("bce03", map.get("bce03").toString());
                //试管列表(门诊分检查和检验两种)
                List tempList = new ArrayList();
                if("L".equals(bda01))
                {
                  //如果相同试管和相同样本相同医嘱单和相同执行科室，用相同的条码
                    List<Map<String, Object>> hasList = new ArrayList<Map<String, Object>>();
                    tempList = cliDoctorDao.getTempListL_batch(params);
                    //得到条码的最后序列号
                    for(int j = 0;j<tempList.size();j++)
                    {
                        Map bel1 = (Map)tempList.get(j);
                        bel1.put("vbq01", getNewId.nextId());
                        bel1.put("cbm01", cbm01);
                        bel1.put("bda01", bda01);
                        String vbi21 = GlobalVariable.StringIsNull(hasBel1(hasList, bel1, cbw03, para89).get("vbi21"));
                        cbw03 = GlobalVariable.IntegerIsNull(hasBel1(hasList, bel1, cbw03, para89).get("cbw03"));
                        //条码对应的数值
                        bel1.put("vbi21",vbi21);
                        hasList.add(bel1);
                    }
                    params.put("bel1List", tempList);
                    //插入诊疗申请单
                    cliDoctorDao.insertVbq1_batch(params);
                    //更新医嘱记录中的申请单id
                    cliDoctorDao.updateVaf1_batch(params);
                    //更新抄送记录表
                    cliDoctorDao.updateVbi1_batch(params);
                }
                else if("E".equals(bda01))
                {
                    //如果相同试管和相同样本相同医嘱单和相同执行科室，用相同的条码
                    List<Map<String, Object>> hasList = new ArrayList<Map<String, Object>>();
                    tempList = cliDoctorDao.getTempListE_batch(params);
                    //得到条码的最后序列号
                    for(int j = 0;j<tempList.size();j++)
                    {
                        Map bel1 = (Map)tempList.get(j);
                        bel1.put("vbq01", getNewId.nextId());
                        bel1.put("cbm01", cbm01);
                        bel1.put("bda01", bda01);
                        String vbi21 = GlobalVariable.StringIsNull(hasBel2(hasList, bel1, cbw03, para89).get("vbi21"));
                        cbw03 = GlobalVariable.IntegerIsNull(hasBel2(hasList, bel1, cbw03, para89).get("cbw03"));
                        bel1.put("vbi21",vbi21);
                        bel1.put("vbq27",vbqMap.get("vbq27"));
                        bel1.put("vbq28",vbqMap.get("vbq28"));
                        bel1.put("vbq29",vbqMap.get("vbq29"));
                        bel1.put("vbq30",vbqMap.get("vbq30"));
                        hasList.add(bel1);
                    }
                    params.put("bel1List", tempList);
                    //插入诊疗申请单
                    cliDoctorDao.insertVbq1_E_batch(params);
                    //更新医嘱记录中的申请单id
                    cliDoctorDao.updateVaf1_E_batch(params);
                    //更新抄送记录表
                    cliDoctorDao.updateVbi1_E_batch(params);
                }
            }
            mo.setMap(map);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
    private String getTemp9_14(int cbw03)
    {
        int length = 6-(cbw03+"").length();
        String temp9_14 = "";
        for(int k = 0;k<length;k++)
        {
            temp9_14 += "0";
        }
        return temp9_14;
    }
    
    public void insertCbw03()
    {
            int cbw03 = 1;
            List temp1List;
            try
            {
                Map teMap = new HashMap<>();
                temp1List = cliDoctorDao.getTemp1List(teMap);
                if(temp1List.size()<=0)
                {
                    Map map = new HashMap<>();
                    map.put("cbw01", UUID.randomUUID().toString().toUpperCase());
                    cliDoctorDao.insertCbw1(map);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    }
    
    public void updateCbw03(int cbw03)
    {
        try
        {
            cliDoctorDao.updateCbw1(cbw03);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    @SuppressWarnings("unchecked")
    public String getParameter(Integer productId, Integer programId, Integer paramNo)
        throws Exception
    {
        HashMap map = new HashMap();
        map.put("productid", productId);
        map.put("programid", programId);
        map.put("paramno", paramNo);
        return cliParamterDao.getSysParamValue(map);
    }

    public ModelVo getGeiyaoZhixing(HashMap map)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.getGeiyaoZhixing(map);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setList(list);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo getBce03(HashMap map)
    {
        ModelVo mv = new ModelVo();
        try
        {
            Map temp = cliDoctorDao.getBce03(map);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setMap(temp);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }
    
    /**
	 * 获取门诊号
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelVo horategetvaa03(){
	    synchronized(CliDoctorBo.class){
		ModelVo mv  = new ModelVo();
		String VAA03="";
        Integer aID = null;
        String aLStr;
        Integer aLen;
        String aStrB;
        aStrB="00000000000000000000";
        try {
        	Map temp1= new HashMap<>();
        	temp1.put("productid", 100);
        	temp1.put("programid", 103005);
        	temp1.put("paramno", 39);
        	Map temp2= new HashMap<>();
        	temp2.put("productid", 100);
        	temp2.put("programid", 103005);
        	temp2.put("paramno", 40);
			aLStr = GlobalVariable.StringIsNull((cliParamterDao.getSysParamValue(temp1)));
			 aLen=cliParamterDao.getSysParamValue(temp2)==null ? GlobalVariable.IntegerIsNull("6") : GlobalVariable.IntegerIsNull((cliParamterDao.getSysParamValue(temp2)));
		        if(aLStr.toUpperCase().equals("YYYY")){
		        	aLStr=aLStr.substring(0, 3);
		        }
		        if(aLen < 6){
		        	aLen=6;
		        }
		    	Integer  IncCount=1;
		    	cliDoctorDao.updatenewvaa03(IncCount);
		    	List list=cliDoctorDao.selectVAA1ID3();
		    	if(list != null && list.size() > 0){
		    		aID=GlobalVariable.IntegerIsNull(((Map)list.get(0)).get("value"));
		    		if(aID==0){
		    			aID=1;
		    			cliDoctorDao.insertnewvaa03();
		    		}
		    	}
        	VAA03=aLStr+aStrB.substring(0, aLen-aLStr.length() - (aID+"").length())+aID+"";
		} catch (Exception e) {
			mv.setResult(StaticKeys.ERROR);
			e.printStackTrace();
		}
       
    	Map map=new HashMap();
    	List resultlist=new ArrayList();
    	map.put("VAA03", VAA03);
    	resultlist.add(map);
    	mv.setList(resultlist);
    	mv.setMap(map);
    	mv.setResult(StaticKeys.SUCCESS);
        return mv;
	    }
    }

    public ModelVo queryBce1Info(Map paramMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryBce1Info(paramMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setList(list);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo abb1()
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.abb1", null);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setList(list);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo getYiyuanInfo()
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.getYiyuanInfo("CliDoctor.getYiyuanInfo", null);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setList(list);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }
    
    //WJT 根据会员卡号查询病人信息接口
    /**
     * 根据会员卡号查询病人信息
     * @return
     */
    @SuppressWarnings("unchecked")
    public ModelVo queryVac1ByVcb(Map params)
    {
        ModelVo mv = new ModelVo();
        try
        {
            Object obj = cliDoctorDao.queryVac1ByVcb("CliDoctor.queryVac1ByVcb", params);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setMap((Map)obj);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    
    /**
     * @return
     */
    public ModelVo queryJibingByText(Map params)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.queryJibingByText", params);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setList(list);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo queryFileInfoByType(Map teMap) throws Exception
    {
        ModelVo mv = new ModelVo();
        List list = cliDoctorDao.queryForList("CliDoctor.queryFileInfoByType", teMap);
        mv.setResult(StaticKeys.OH_SUCCESS);
        mv.setList(list);
        return mv;
    }

    public ModelVo script817(Map paramMap) throws Exception
    {
        ModelVo mv = new ModelVo();
        List list = cliDoctorDao.queryForList("CliDoctor.script817", paramMap);
        mv.setResult(StaticKeys.OH_SUCCESS);
        mv.setList(list);
        return mv;
    }

    /**
     * 
     * @param teMap
     * @return
     * @throws Exception
     */
    public ModelVo queryHasZj(Map teMap) throws Exception
    {
        ModelVo mv = new ModelVo();
        List list;
        try
        {
            list = cliDoctorDao.queryForList("CliDoctor.queryZjByvac01", teMap);
            Map<String, Object> map = new HashMap<String, Object>();
            //当天是否有交过诊查费
            if (list==null || list.isEmpty())
            {
                mv.setResult(StaticKeys.OH_SUCCESS);
                map.put("flag", true);
                mv.setMap(map);
            }
            else
            {
                teMap.put("vaj46", ((Map)list.get(0)).get("vaj46"));
                List result = cliDoctorDao.queryForList("CliDoctor.queryHasZj", teMap);
                double num = 0;
                if (result != null && !result.isEmpty())
                {
                    num = Double.parseDouble(((Map<String, Object>)(result.get(0))).get("num").toString());
                }
                //当天的诊金是否超过4小时
                if (teMap.get("hours") !=null && num >= Integer.parseInt(teMap.get("hours").toString()))
                {
                    mv.setResult(StaticKeys.OH_SUCCESS);
                    map.put("flag", true);
                    mv.setMap(map);
                }
                else
                {
                    mv.setResult(StaticKeys.OH_SUCCESS);
                    map.put("flag", false);
                    mv.setMap(map);
                }
            } 
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
        return mv;
    }

    public ModelVo queryZjList(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.queryZjList", teMap);
            mv.setList(list);
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo queryZhentaiConfig(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.queryZhentaiConfig", teMap);
            mv.setList(list);
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo savePatientMore(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            cliDoctorDao.update("CliDoctor.savePatientMore", teMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo selecthuiyuanbingren(Map<String, Object> teMap, int currentPage, int pageSize)
    {
        ModelVo mv = new ModelVo();
        try
        {
            Page page = cliDoctorDao.queryForPage("CliDoctor.selecthuiyuanbingren", teMap, currentPage, pageSize);
            mv.setList(page.getList());
            mv.setRows(page.getList());
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo selectDataByScriptId1(HashMap param)
    {
            ModelVo mv = new ModelVo();
            List list = new ArrayList();
            try {
                String xText = ratevaa1Dao.querySysScriptSql(param);
                String paramStr = (String) param.get("paramStr");
                if (paramStr != null && !paramStr.contains("{")) {
                    paramStr = "{" + paramStr + "}";
                }
                if (paramStr != null) {
                    JSONObject json = JSONObject.fromObject(paramStr);
                    Iterator<String> keyIter = json.keys();
                    String key;
                    Object value;
                    while (keyIter.hasNext()) {
                        key = (String) keyIter.next();
                        value = json.get(key);
                        if (value != null && value.toString().contains("^")) {
                            value = value.toString().replace("^", ":");
                        }
                        if (value instanceof String) {
                            value = "'" + value + "'";
                        }
                        /*if ((Integer) param.get("ID") == 997 && key.equals("VAA03")) {
                            xText = xText.replaceAll("@context", " (a.VAA03 like "+value+" OR a.VAA03 like "+value.toString().toUpperCase()+" or a.VAA05 like "+value+" or a.ABBRP like "+value+")");
                        }else if ((Integer) param.get("ID") == 997 && key.equals("VAA05")) {
                            xText = xText.replaceAll("@context", " (a.VAA05 like "+value+" or a.ABBRP like "+value+")");
                        }else if ((Integer) param.get("ID") == 997 && key.equals("ABBRW")) {
                            xText = xText.replaceAll("@context", " a.ABBRW like "+value+"");
                        }else if ((Integer) param.get("ID") == 997 && key.equals("ABBRP")) {
                            xText = xText.replaceAll("@context", " a.ABBRP like "+value+"");
                        }else if ((Integer) param.get("ID") == 997 && key.equals("VAA15")) {
                            xText = xText.replaceAll("@context", " a.VAA15 like "+value+"");
                        }else if ((Integer) param.get("ID") == 997 && key.equals("VAA02")) {
                            xText = xText.replaceAll("@context", " a.VAA02 like "+value+"");
                        }else if ((Integer) param.get("ID") == 997 && key.equals("VAA35")) {
                            xText = xText.replaceAll("@context", " a.VAA35 like "+value+"");
                        }else if ((Integer) param.get("ID") == 997 && key.equals("VAA84")) {
                            xText = xText.replaceAll("@context", " a.VAA84 like "+value+"");
                        }*/
                        
                        if ((Integer) param.get("ID") == 997) {
                            xText = xText.replaceAll("@context", " (a.VAA03 like "+value+" or a.ABBRW like "+value+" "
                                + "or a.VAA05 like "+value+" or a.ABBRP like "+value+" or a.VAA15 like "+value+" or a.VAA02 like "+value+" "
                                    + "or a.VAA35 like "+value+" or a.VAA84 like "+value+" or a.VAA18 like "+value+")");
                        }
                    }
                }
                param.put("xText", xText);
                //list = ratevaa1Dao.queryPageByParams(param);
               // ratevaa1Dao.queryPageByParams(param);
                Page page = cliDoctorDao.queryPageByParams(param, GlobalVariable.IntegerIsNull(param.get("page")), GlobalVariable.IntegerIsNull(param.get("rows")));
                mv.setRecords(page.getTotalNumber());
                mv.setRows(page.getList());
                mv.setTotal(page.getTotalNumber());
                mv.setList(page.getList());
            } catch (Exception e) {
                e.printStackTrace();
                throw ExBaseException.getDetailMessage(e);
            }
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }

    public ModelVo querybas02Bybce01(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.querybas02Bybce01", teMap);
            mv.setList(list);
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo querybbx20Byvaf01(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.querybbx20Byvaf01", teMap);
            mv.setList(list);
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo updateBas1_bce1(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.hasBas1_bce1", teMap);
            if(list!=null && !list.isEmpty())
            {
                cliDoctorDao.update("CliDoctor.updateBas1_bce1", teMap);
            }
            else
            {
                teMap.put("id", getNewId.nextId());
                cliDoctorDao.update("CliDoctor.insertBas1_bce1", teMap);
            }
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public ModelVo updateHujiao_houtai(HashMap hs) throws Exception
    {
        synchronized(CliDoctorBo.class){
        ModelVo mv = new ModelVo();
            HashMap map = new HashMap();
            map.put("productid", 100);
            map.put("programid", 105002);
            map.put("paramno", 238);
            String value = cliParamterDao.getSysParamValue(map);
            
            hs.put("lPara2", value == null || "".equals(value) ? "0" : value);
            //检索候诊医生的列表
            List list = dao.querywaitingandtransfertreatment_one(hs);
            if(list==null || list.isEmpty())
            {
                mv.setResult(StaticKeys.OH_FALSE);  //falil
                mv.setResultMsg("no-body");
                return mv;
            }
            //检查要求年龄的血压是否正确
            Map map2 = new HashMap();
            map2.put("productid", 1);
            map2.put("programid", 117001);
            map2.put("paramno", 1);
            Integer value2 = GlobalVariable.IntegerIsNull((cliParamterDao.getSysParamValue(map2)));
            Map map3 = new HashMap();
            map3.put("productid", 1);
            map3.put("programid", 117001);
            map3.put("paramno", 2);
            Integer value3 = GlobalVariable.IntegerIsNull((cliParamterDao.getSysParamValue(map3)));
            //复诊病人不测血压
            Map map5 = new HashMap();
            map5.put("productid", 100);
            map5.put("programid", 105002);
            map5.put("paramno", 430);
            Integer is_not_testblood = GlobalVariable.IntegerIsNull((cliParamterDao.getSysParamValue(map5)));
            
            //复诊病人不测体重
            Map map6 = new HashMap();
            map6.put("productid", 100);
            map6.put("programid", 105002);
            map6.put("paramno", 431);   
            Integer not_test_hit = GlobalVariable.IntegerIsNull((cliParamterDao.getSysParamValue(map6)));
            
            //拿到第一个病人(或者指定的病人)
            Map<String,Object> patientMap = new HashMap<String,Object>();
            String vac01 = GlobalVariable.StringIsNull(hs.get("vac01"));
            if("".equals(vac01))
            {
                patientMap = (Map<String,Object>)list.get(0); 
            }
            else
            {
                for(int i = 0;i<list.size();i++)
                {
                    Map<String,Object> temp = (Map<String,Object>)list.get(i);
                    if(temp.get("vac01").equals(vac01))
                    {
                        patientMap = (Map<String,Object>)list.get(i); 
                    }
                }
            }
            //验证血压
            String age = GlobalVariable.StringIsNull(patientMap.get("agep"));
            int vac06 = GlobalVariable.IntegerIsNull(patientMap.get("vac06"));
            if(age.contains("岁"))
            {
                Integer ageNum = GlobalVariable.IntegerIsNull(age.split("岁")[0]);
                    if(ageNum >= value2)
                    {
                        if(!(vac06==1 && is_not_testblood==1))   //must test blood
                        {
                            Double vcj04 = GlobalVariable.DoubleIsNull(patientMap.get("vcj04"));
                            Double vcj05 = GlobalVariable.DoubleIsNull(patientMap.get("vcj05"));
                            if(vcj04==0.0 || vcj05==0.0)
                            {
                                mv.setMap(patientMap);
                                mv.setResult(StaticKeys.OH_FALSE);  //ati
                                mv.setResultMsg("bad-blood");
                                mv.setTotal(value2);
                                //
                                //ati
                                mv.setList(list);
                                return mv;
                            }
                        }
                }
                //验证体重  if age<12
                else if(ageNum<=value3)
                {
                    if (!(vac06==1 && not_test_hit==1))  //must test hti
                    {
                        Double vac63 = GlobalVariable.DoubleIsNull(patientMap.get("vac63"));
                        if (vac63 == 0.0)
                        {
                            mv.setMap(patientMap);
                            mv.setResult(StaticKeys.OH_FALSE);  //ati
                            mv.setResultMsg("bad-tizhong");
                            //ati
                            mv.setList(list);
                              return mv; // ati not return nex resume
                        } 
                    }
                }
            }
            //验证体重
            else if(age.contains("月") || age.contains("天"))
            {
                Double vac63 = GlobalVariable.DoubleIsNull(patientMap.get("vac63"));
                if(vac63==0.0)
                {
                    if (!(vac06==1 && not_test_hit==1))
                    {
                        mv.setMap(patientMap);
                        mv.setResult(StaticKeys.OH_FALSE);  //ati
                        mv.setResultMsg("bad-tizhong");
                        //ati
                        mv.setList(list);
                       return mv;
                    }
                }
            }
            
            //接诊
            hs.put("lVAA07", GlobalVariable.LongIsNull(patientMap.get("vac01")));
            hs.put("lVAA01", GlobalVariable.LongIsNull(patientMap.get("vaa01")));
            clinicalReceptionBo.execClinicVAC1Update(hs);
            Map map4 = new HashMap();
            map4.put("productid", 9999);
            map4.put("programid", 9999);
            map4.put("paramno", 113);
            Integer value4 = GlobalVariable.IntegerIsNull((cliParamterDao.getSysParamValue(map4)));
          //保存诊金
            //生成诊金的单据和费用
            List<Map<String,Object>> zjAddList = JsonUtil.getList(GlobalVariable.StringIsNull(hs.get("zhenjinVajList")));
            List<Map<String,Object>> zjVakList = new ArrayList<Map<String,Object>>();
            if(zjAddList!=null && !zjAddList.isEmpty())
            {
                List<Long> vaj01s = getNewId.nextId(zjAddList.size());
                long vai01 = getNewId.nextId();
                for(int i =0;i< zjAddList.size();i++)
                {
                    Map<String,Object> vajMap = zjAddList.get(i);
                    vajMap.put("vaa01", map.get("vaa01"));
                    vajMap.put("vaa07", map.get("vac01"));
                    vajMap.put("bck01", map.get("bck01"));
                    vajMap.put("bce02", map.get("bce02_z"));
                    vajMap.put("bce03", map.get("bce03"));
                    vajMap.put("abc02", map.get("abc02"));
                    vajMap.put("vaj01", vaj01s.get(i));
                    Map<String,Object> zjVakMap = new HashMap<String,Object>();
                    zjVakMap.put("VAA01",map.get("vaa01"));
                    zjVakMap.put("VAA07",map.get("vaca01"));
                    zjVakMap.put("VAI01",vai01);
                    zjVakMap.put("ACF01",1);
                    zjVakMap.put("BCE01",map.get("bce03"));// 操作员id
                    zjVakMap.put("BCK01",map.get("bck01"));// 科室id
                    zjVakMap.put("BCE02",map.get("bce02_z"));// 操作员code
                    zjVakMap.put("BCE02",map.get("bce03"));// 操作员姓名
                    zjVakMap.put("VAJ38",map.get("bby25"));// 基本账户扣费
                    zjVakMap.put("AType",0);// 0 = 扣费,1 = 退费
                    zjVakMap.put("AWorkType",1);// 门诊记账
                    zjVakList.add(zjVakMap);
                }
                Map<String,Object> zjMap = new HashMap<String,Object>();
                zjMap.put("vai01", vai01);
                zjMap.put("vaa01", map.get("lVAA01"));
                zjMap.put("vaa07", map.get("lVAA07"));
                zjMap.put("bck01", map.get("lBCK01"));
                zjMap.put("bce02", map.get("lBCE02"));
                zjMap.put("bce03", map.get("lBCE03"));
                zjMap.put("zjAddList", zjAddList);
                cliSendAdviceDao.saveZj(zjMap);
            }
            if(value4==1)
            {
                //调用一卡通扣费
                for(Map<String, Object> temp0 : zjVakList)
                {
                    cardMemberMangerBo.updatehoratecardcharge(temp0);
                }
            }
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setMap(patientMap);
            mv.setList(list);
        return mv;
        }
    }

    @SuppressWarnings("rawtypes")
    public ModelVo updateVaj1(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            //校验收费项目是否已经结账
            List list = cliDoctorDao.queryForList("CliDoctor.queryVajVakList", teMap);
            if(list!=null && !list.isEmpty())
            {
                mv.setResult(StaticKeys.OH_FALSE);
                mv.setResultMsg("jiezhang");
                return mv;
            }
            else
            {
                cliDoctorDao.update("CliDoctor.updateVaj1", teMap);
                mv.setResult(StaticKeys.OH_SUCCESS);
                return mv;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo updateVaa15ByVac01(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            cliDoctorDao.update("CliDoctor.updateVaa15ByVac01", teMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo querySendZjByvac01(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.querySendZjByvac01", teMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setList(list);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo updateVajZjByvaj01(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            //修改诊金费用前先检测是否已经结过账
            List list = cliDoctorDao.queryForList("CliDoctor.queryZjIsJiezhang", teMap);
            if(list==null || list.isEmpty())
            {
                //修改诊金费用信息
                cliDoctorDao.update("CliDoctor.updateVaj1ByVaj01", teMap);
                mv.setResult(StaticKeys.OH_SUCCESS);
                return mv;
            }
            else
            {
                mv.setResult(StaticKeys.OH_FALSE);
                mv.setResultMsg("病人的诊金项目已经结账，不能修改！");
                return mv;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResultMsg(e.toString());
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo getIpAndMac(Map<String, Object> teMap, HttpServletRequest request)
    {
        ModelVo mv = new ModelVo();
        try
        {
            String ip = CLoginFilter.getIpAddress(request);
//            String mac = CLoginFilter.getMACAddress(ip);
            String mac = "";
            Map<String,Object> temp = new HashMap<String,Object>();
            temp.put("ip", ip);
            temp.put("mac", mac);
            mv.setMap(temp);
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResultMsg(e.toString());
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo getHccode(HashMap map)
    {
        ModelVo mv = new ModelVo();
        try
        {
            Map temp = cliDoctorDao.getHccode(map);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setMap(temp);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo querySysTime()
    {
        ModelVo mv = new ModelVo();
        try
        {
            Map temp = cliDoctorDao.querySysTime(new HashMap<>());
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setMap(temp);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public int updateCbw03_(int i)
    {
        try
        {
            return cliDoctorDao.updateCbw1_(i);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return 0;
        }
    }

    public ModelVo queryBce1_vao1(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.queryBce1_vao1", teMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setList(list);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }
    public ModelVo updateBce01_vao1(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = (List<Map<String, Object>>)teMap.get("vaoList");
            List<Long> ids = getNewId.nextId(list.size());
            for(int i=0;i<list.size();i++) 
            {
                Map map = (Map<String,Object>)list.get(i);
                map.put("id", ids.get(i));
                map.put("abbrp", PinyinHelper.getShortPinyin(GlobalVariable.StringIsNull(map.get("bak05"))));
            }
            cliDoctorDao.update("CliDoctor.updateBce01_vao1", teMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo queryFamilyRelation(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.queryFamilyRelation", teMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setList(list);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo updateBdj1ByBce01AndBbx01(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            cliDoctorDao.update("CliDoctor.updateBdj1ByBce01AndBbx01", teMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }
    
    public ModelVo deleteBdj1ByBce01AndBbx01(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            cliDoctorDao.update("CliDoctor.deleteBdj1ByBce01AndBbx01", teMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo queryAah1Bby1ByBce01(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.queryAah1Bby1ByBce01", teMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setList(list);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo queryEye1(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List list = cliDoctorDao.queryForList("CliDoctor.queryEye1", teMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            mv.setList(list);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo saveEye1(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            cliDoctorDao.insert("CliDoctor.saveEye1", teMap);
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

    public ModelVo updateEye1(Map<String, Object> teMap)
    {
        ModelVo mv = new ModelVo();
        try
        {
            List<Map<String, Object>> ykList = (List<Map<String, Object>>)teMap.get("ykList");
            Map<String, Object> map = new HashMap<String, Object>();
            for(int i=0;i<ykList.size();i++)
            {
                Map<String, Object> temp = ykList.get(i);
                if(temp.get("value")!=null)
                {
                    map.put(GlobalVariable.StringIsNull(temp.get("name")), temp.get("value"));
                }
            }
            List hasList = cliDoctorDao.queryForList("CliDoctor.queryEye1", map);
            if(hasList!=null && !hasList.isEmpty())
            {
                cliDoctorDao.update("CliDoctor.updateEye1", map);
            }
            else
            {
                cliDoctorDao.insert("CliDoctor.saveEye1", map);
            }
            mv.setResult(StaticKeys.OH_SUCCESS);
            return mv;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mv.setResult(StaticKeys.OH_FALSE);
            return mv;
        }
    }

}
